package com.hdjk.view.activity.personalcenter;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hdjk.view.R;
import com.hdjk.view.callback.CallBackListener;
import com.hdjk.view.network.Network;
import com.hdjk.view.network.PersonalNetWork;
import com.hdjk.view.util.ToastUtils;
import com.hdjk.view.util.UserTokenUtil;
import com.lst.projectlib.base.BaseActivity;
import com.lst.projectlib.entity.Msg;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 忘记密码
 * Created by Administrator on 2017/11/14.
 */

public class ForgetPwdActivity extends BaseActivity implements View.OnClickListener {
    public static final int PWD_TYPE_LOGIN = 1;//密码类型，登录密码
    public static final int PWD_TYPE_TRAN = 2;//密码类型，交易密码

    private int pwdType = 0;

    // Content View Elements
    private com.lst.projectlib.component.CustomView.TopTitleBar ttb_title_bar;
    private RelativeLayout rel_id_number;
    private EditText et_id_number;
    private View line_id_number;
    private RelativeLayout rel_id_name;
    private EditText et_id_name;
    private View line_id_name;
    private RelativeLayout rel_phone;
    private EditText et_phone;
    private View line_phone;
    private EditText et_code;
    private TextView tv_send_code;
    private EditText et_new_pwd;
    private TextView tv_sure;
    // End Of Content View Elements

    private void bindViews() {
        ttb_title_bar = (com.lst.projectlib.component.CustomView.TopTitleBar) findViewById(R.id.ttb_title_bar);
        rel_id_number = (RelativeLayout) findViewById(R.id.rel_id_number);
        et_id_number = (EditText) findViewById(R.id.et_id_number);
        line_id_number = findViewById(R.id.line_id_number);
        rel_id_name = (RelativeLayout) findViewById(R.id.rel_id_name);
        et_id_name = (EditText) findViewById(R.id.et_id_name);
        line_id_name = findViewById(R.id.line_id_name);
        rel_phone = (RelativeLayout) findViewById(R.id.rel_phone);
        et_phone = (EditText) findViewById(R.id.et_phone);
        line_phone = findViewById(R.id.line_phone);
        et_code = (EditText) findViewById(R.id.et_code);
        tv_send_code = (TextView) findViewById(R.id.tv_send_code);
        et_new_pwd = (EditText) findViewById(R.id.et_new_pwd);
        tv_sure = (TextView) findViewById(R.id.tv_sure);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_forget_pwd);
        bindViews();
        ttb_title_bar.findViewById(R.id.tvNavBack).setOnClickListener(this);
        tv_send_code.setOnClickListener(this);
        tv_sure.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        pwdType = getIntent().getIntExtra("pwdType", 0);
        if (pwdType == PWD_TYPE_LOGIN) {
            rel_id_number.setVisibility(View.GONE);
            line_id_number.setVisibility(View.GONE);
            rel_id_name.setVisibility(View.GONE);
            line_id_name.setVisibility(View.GONE);
            rel_phone.setVisibility(View.VISIBLE);
            line_phone.setVisibility(View.VISIBLE);
            et_code.setHint("请输入验证码");
        } else if (pwdType == PWD_TYPE_TRAN) {
            rel_id_number.setVisibility(View.VISIBLE);
            line_id_number.setVisibility(View.VISIBLE);
            rel_id_name.setVisibility(View.VISIBLE);
            line_id_name.setVisibility(View.VISIBLE);
            rel_phone.setVisibility(View.GONE);
            line_phone.setVisibility(View.GONE);
            ((TextView) ttb_title_bar.findViewById(R.id.tvNavTitle)).setText("重置密码");
            tv_sure.setText("确认修改");
            et_code.setHint("验证码");
        }
    }

    private CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            tv_send_code.setText("已发送" + (millisUntilFinished / 1000));
        }

        @Override
        public void onFinish() {
            tv_send_code.setEnabled(true);
            tv_send_code.setBackgroundResource(R.drawable.button_yanzhengma_sel);
            tv_send_code.setTextColor(getResources().getColor(R.color.white));
            tv_send_code.setText("发送验证码");
        }
    };

    @Override
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.tvNavBack:
                back();
                break;
            case R.id.tv_send_code:
                sendCode();
                break;
            case R.id.tv_sure:
                submit();
                break;
        }
    }

    private void sendCode() {
        if (pwdType == PWD_TYPE_LOGIN) {
            String phone = et_phone.getText().toString();
            if (TextUtils.isEmpty(phone)) {
                ToastUtils.showShortToast(this, "请输入手机号");
                return;
            }
            mDialog.show();
            JSONObject obj = new JSONObject();
            try {
                obj.put("Phone", phone);
                obj.put("Type", 0);
                obj.put("CheckExistentOrNo",true);
                Network.getPhoneCode(obj.toString(), new CallBackListener() {
                    @Override
                    public void onCallBackCompleted(Msg msg) {
                        mDialog.cancel();
                        if (msg != null && msg.getSuccess()) {
                            tv_send_code.setBackgroundResource(R.drawable.utton_yanzhengma_dis);
                            tv_send_code.setTextColor(getResources().getColor(R.color.gray_aaaaaaa));
                            tv_send_code.setEnabled(false);
                            timer.start();
                            ToastUtils.showShortToast(ForgetPwdActivity.this, "验证码发送成功");
                        } else {
                            if (msg != null && !TextUtils.isEmpty(msg.getMsg()) && !"null".equals(msg.getMsg())) {
                                ToastUtils.showShortToast(ForgetPwdActivity.this, msg.getMsg());
                            } else {
                                ToastUtils.showShortToast(ForgetPwdActivity.this, "验证码发送失败");
                            }
                        }
                    }

                    @Override
                    public void onCallBackError(Msg msg) {
                        mDialog.cancel();
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else if (pwdType == PWD_TYPE_TRAN) {
            String idNumber = et_id_number.getText().toString();
            String idName = et_id_name.getText().toString();
            if (TextUtils.isEmpty(idNumber)) {
                ToastUtils.showShortToast(this, "请输入证件号码");
                return;
            }
            if (TextUtils.isEmpty(idName)) {
                ToastUtils.showShortToast(this, "请输入证件名称");
                return;
            }
            mDialog.show();
            JSONObject obj = new JSONObject();
            try {
                obj.put("idNumber", idNumber);
                obj.put("idName", idName);
                PersonalNetWork.forgetTranPwdSendCode(UserTokenUtil.getToken(ForgetPwdActivity.this), obj.toString(), new CallBackListener() {
                    @Override
                    public void onCallBackCompleted(Msg msg) {
                        mDialog.cancel();
                        if (msg != null && msg.getSuccess()) {
                            tv_send_code.setBackgroundResource(R.drawable.utton_yanzhengma_dis);
                            tv_send_code.setTextColor(getResources().getColor(R.color.gray_aaaaaaa));
                            tv_send_code.setEnabled(false);
                            timer.start();
                            ToastUtils.showShortToast(ForgetPwdActivity.this, "验证码发送成功");
                        } else {
                            if (msg != null && !TextUtils.isEmpty(msg.getMsg())) {
                                ToastUtils.showShortToast(ForgetPwdActivity.this, msg.getMsg());
                            } else {
                                ToastUtils.showShortToast(ForgetPwdActivity.this, "验证码发送失败");
                            }
                        }
                    }

                    @Override
                    public void onCallBackError(Msg msg) {
                        mDialog.cancel();
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    private void submit() {
        if (pwdType == PWD_TYPE_LOGIN) {
            String phone = et_phone.getText().toString();
            if (TextUtils.isEmpty(phone)) {
                ToastUtils.showShortToast(this, "请输入手机号");
                return;
            }
        } else if (pwdType == PWD_TYPE_TRAN) {
            String idNumber = et_id_number.getText().toString();
            String idName = et_id_name.getText().toString();
            if (TextUtils.isEmpty(idNumber)) {
                ToastUtils.showShortToast(this, "请输入证件号码");
                return;
            }
            if (TextUtils.isEmpty(idName)) {
                ToastUtils.showShortToast(this, "请输入证件名称");
                return;
            }
        }
        String code = et_code.getText().toString();
        final String pwd = et_new_pwd.getText().toString();
        if (TextUtils.isEmpty(code)) {
            ToastUtils.showShortToast(this, "请输入验证码");
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            ToastUtils.showShortToast(this, "请输入密码");
            return;
        }
        mDialog.show();
        if (pwdType == PWD_TYPE_LOGIN) {
            JSONObject obj = new JSONObject();
            try {
                obj.put("NewPwd", pwd);
                obj.put("CheckCode", code);
                obj.put("Phone", et_phone.getText().toString());
                PersonalNetWork.forgetLoginPwd(obj.toString(), new CallBackListener() {
                    @Override
                    public void onCallBackCompleted(Msg msg) {
                        mDialog.cancel();
                        if (msg != null && msg.getSuccess()) {
                            ToastUtils.showShortToast(ForgetPwdActivity.this, "修改密码成功");
                            back();
                        } else {
                            if (msg != null && !TextUtils.isEmpty(msg.getMsg())) {
                                ToastUtils.showShortToast(ForgetPwdActivity.this, msg.getMsg());
                            } else {
                                ToastUtils.showShortToast(ForgetPwdActivity.this, "修改密码失败");
                            }
                        }
                    }

                    @Override
                    public void onCallBackError(Msg msg) {
                        mDialog.cancel();
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else if (pwdType == PWD_TYPE_TRAN) {
            JSONObject obj = new JSONObject();
            try {
                obj.put("accountNumber", UserTokenUtil.getUserName(ForgetPwdActivity.this));
                obj.put("opt", code);
                PersonalNetWork.forgetTranPwdCheckCode(UserTokenUtil.getToken(ForgetPwdActivity.this), obj.toString(), new CallBackListener() {
                    @Override
                    public void onCallBackCompleted(Msg msg) {
                        mDialog.cancel();
                        if (msg != null && msg.getSuccess()) {
                            String token = (String) msg.getData();
                            JSONObject obj = new JSONObject();
                            try {
                                obj.put("token", token);
                                obj.put("newPassword", pwd);
                                PersonalNetWork.forgetTranPwdResetPwd(UserTokenUtil.getToken(ForgetPwdActivity.this), obj.toString(), new CallBackListener() {
                                    @Override
                                    public void onCallBackCompleted(Msg msg) {
                                        if (msg != null && msg.getSuccess()) {
                                            ToastUtils.showShortToast(ForgetPwdActivity.this, "修改密码成功");
                                            back();
                                        } else {
                                            if (msg != null && !TextUtils.isEmpty(msg.getMsg())) {
                                                ToastUtils.showShortToast(ForgetPwdActivity.this, msg.getMsg());
                                            } else {
                                                ToastUtils.showShortToast(ForgetPwdActivity.this, "修改密码失败");
                                            }
                                        }
                                    }

                                    @Override
                                    public void onCallBackError(Msg msg) {

                                    }
                                });
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        } else {
                            if (msg != null && !TextUtils.isEmpty(msg.getMsg())) {
                                ToastUtils.showShortToast(ForgetPwdActivity.this, msg.getMsg());
                            } else {
                                ToastUtils.showShortToast(ForgetPwdActivity.this, "修改密码失败");
                            }
                        }
                    }

                    @Override
                    public void onCallBackError(Msg msg) {
                        mDialog.cancel();
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}
